/**
 * 请求日志中间件
 */
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
    // 初始化一个日志类
    logger = new Logger('接口请求');
    // 处理函数
    use(req: Request, res: Response, next: NextFunction) {
        const start_time = Date.now();

        res.once('finish', () => {
            const duration = Date.now() - start_time;
            this.logger.verbose(`${req.method} ${req.url} ${res.statusCode} ${duration}ms`);
        });
        next();
    }
}
